package app;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import utils.StringUtils;

import java.io.IOException;

/**
 * Create by zhh on 2022/9/9.
 * 获取高德IP定位功能的测试
 *
 */
public class GaodeAPP {
    public static void main(String[] args) {
        String ip ="210.37.211.200";
        String url="https://restapi.amap.com/v3/ip?ip="+ip+"&output=json&key="+ StringUtils.GAODE_KEY;
        String province = "-";
        String city = "-";
        CloseableHttpClient httpClient = HttpClients.createDefault();//使用HTTP连接
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();//状态码
            if (statusCode==200){
                HttpEntity entity = response.getEntity();//获取实体
                String result = EntityUtils.toString(entity);//获取实体内容
                JSONObject jsonObject = JSON.parseObject(result);//解析json文件
                province = jsonObject.getString("province");
                city = jsonObject.getString("city");
                System.out.println(province+"\t"+city);
            }



        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null!=response){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
